/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework
 * Version 2.10
 * 
 * ODBC Data Provider
 *      Provides a definition for creating ODBC-specific data object instances.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Data;
using System.Data.Sql;
using System.Data.Odbc;
using System.Data.Common;
using Adaptive.Foundation.Properties;

namespace Adaptive.Foundation.Data
{
    /// <summary>
    /// Provides a definition for creating ODBC-specific data object instances.
    /// </summary>
    public sealed class OdbcDataProvider : IDataProvider
    {
        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
		 *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public OdbcDataProvider()
            : base()
        {
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
		 *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Creates a platform-specific connection object.
        /// </summary>
        /// <returns>
        /// A new <see cref="OdbcConnection"/> instance.
        /// </returns>
        public IDbConnection CreateConnection()
        {
            return new OdbcConnection();
        }
        /// <summary>
        /// Creates a platform-specific connection object.
        /// </summary>
        /// <param name="connectionInformation">
        /// The connection string to use when connecting to the database.
        /// </param>
        /// <returns>
        /// A new <see cref="OdbcConnection"/> instance.
        /// </returns>
        public IDbConnection CreateConnection(string connectionInformation)
        {
            return new OdbcConnection(connectionInformation);
        }
        /// <summary>
        /// Creates a platform-specific command-execution object.
        /// </summary>
        /// <param name="connection">
        /// A <see cref="OdbcConnection"/> implementation used to connect to a database.
        /// </param>
        /// <returns>
        /// A new <see cref="OdbcCommand"/> instance.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Occurs when the parameter is not a <see cref="OdbcConnection"/> instance.
        /// </exception>
        public IDbCommand CreateCommand(IDbConnection connection)
        {
            OdbcConnection myConnection = null;

            myConnection = connection as OdbcConnection;
            if (myConnection == null)
                throw new ArgumentException(Resources.InvalidDataConnectTypeMsg, Resources.ConnectionArgumentName);

            return new OdbcCommand(string.Empty, myConnection);

        }
        /// <summary>
        /// Creates a platform-specific command-execution object.
        /// </summary>
        /// <param name="connection">
        /// An <see cref="OdbcConnection"/> implementation used to connect to a database.
        /// </param>
        /// <param name="commandText">
        /// The SQL or other command text.
        /// </param>
        /// <returns>
        /// A new <see cref="OdbcCommand"/> instance.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Occurs when the parameter is not a <see cref="OdbcConnection"/> instance.
        /// </exception>
        public IDbCommand CreateCommand(string commandText, IDbConnection connection)
        {
            OdbcConnection myConnection = null;

            myConnection = connection as OdbcConnection;
            if (myConnection == null)
                throw new ArgumentException(Resources.InvalidDataConnectTypeMsg, Resources.ConnectionArgumentName);

            return new OdbcCommand(commandText, myConnection);
        }
        /// <summary>
        /// Creates a platform-specific data adapter object.
        /// </summary>
        /// <param name="command">
        /// An <see cref="OdbcCommand"/> to be executed.
        /// </param>
        /// <returns>
        /// A new <see cref="OdbcDataAdapter"/> instance.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Occurs when the parameter is not a <see cref="OdbcCommand"/> instance.
        /// </exception>
        public IDataAdapter CreateAdapter(IDbCommand command)
        {
            OdbcCommand myCommand = null;

            myCommand = command as OdbcCommand;
            if (myCommand == null)
                throw new ArgumentException(Resources.InvalidDataConnectTypeMsg, Resources.ConnectionArgumentName);

            return new OdbcDataAdapter(myCommand);
        }
        /// <summary>
        /// Creates a platform-specific <see cref="IDataParameter"/>-derived instance.
        /// </summary>
        /// <returns>
        /// An <see cref="OdbcParameter"/> instance.
        /// </returns>
        public IDataParameter CreateParameterInstance()
        {
            return new OdbcParameter();
        }
        /// <summary>
        /// Creates a platform-specific <see cref="IDataParameter"/>-derived instance.
        /// </summary>
        /// <param name="parameterName">The name of the parameter.</param>
        /// <param name="parameterType">The data type of the parameter.</param>
        /// <param name="direction">The direction of the parameter.</param>
        /// <returns>
        /// An <see cref="IDataParameter"/>-derived instance.
        /// </returns>
        public IDataParameter CreateParameterInstance(string parameterName, DbType parameterType, ParameterDirection direction)
        {
            OdbcParameter returnValue = null;

            returnValue = new OdbcParameter(parameterName, parameterType);
            returnValue.DbType = parameterType;
            returnValue.Direction = direction;
            return returnValue;
        }
        /// <summary>
        /// Creates a platform-specific <see cref="IDbTransaction"/>-derived instance.
        /// </summary>
        /// <param name="parentConnection">
        /// The connection instance from which the transaction originates.
        /// </param>
        /// <returns>
        /// An <see cref="IDbTransaction"/>-derived instance.
        /// </returns>
        public IDbTransaction CreateTransactionInstance(IDbConnection parentConnection)
        {
            return CreateTransactionInstance(parentConnection, string.Empty);
        }
        /// <summary>
        /// Creates a platform-specific <see cref="IDbTransaction"/>-derived instance.
        /// </summary>
        /// <param name="parentConnection">
        /// The connection instance from which the transaction originates.
        /// </param>
        /// <param name="transactionName">
        /// A name to assign to the transaction instance.
        /// </param>
        /// <returns>
        /// An <see cref="IDbTransaction"/>-derived instance.
        /// </returns>
        public IDbTransaction CreateTransactionInstance(IDbConnection parentConnection, string transactionName)
        {
            OdbcTransaction returnValue = null;

            if (parentConnection == null)
                throw new ArgumentNullException(Resources.ConnectionArgumentName);
            if (parentConnection.GetType() != typeof(OdbcConnection))
                throw new ArgumentException("This operation requires an OdbcConnection data type.", Resources.ConnectionArgumentName);

            returnValue = (OdbcTransaction)parentConnection.BeginTransaction();

            return returnValue;
        }
        /// <summary>
        /// Creates a connection string builder for the database.
        /// </summary>
        /// <returns>
        /// A <see cref="OdbcConnectionStringBuilder"/> instance for creating a connection string.
        /// </returns>
        public DbConnectionStringBuilder CreateConnectStringBuilder()
        {
            return new OdbcConnectionStringBuilder();
        }
        /// <summary>
        /// Creates a connection string builder for the database.
        /// </summary>
        /// <param name="connectString">The connect string to be parsed.</param>
        /// <returns>
        /// A <see cref="OdbcConnectionStringBuilder"/> instance for parsing and/or creating a connection string.
        /// </returns>
        public DbConnectionStringBuilder CreateConnectStringBuilder(string connectString)
        {
            return new OdbcConnectionStringBuilder(connectString);
        }
        #endregion
    }
}